<?php

/**
 * URole.php
 *
 * @author: wjp 2017-04-29
 */
class Model_URole extends PhalApi_Model_NotORM
{
    /**
     * 根据主键值返回对应的表名，注意分表的情况
     */
    protected function getTableName($id)
    {
        return 'u_role';
    }

    /**
     * 重写getORM方法，方便下面使用
     */
    protected function getORM($id = NULL)
    {
        return parent::getORM($id == NULL ? 'we_base' : $id);
    }


    public function __construct()
    {
    }


    /**
     * 获取角色信息
     * @param array $data
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function getRole($data)
    {
        try {
            $sql = "select * from u_role where id = {$data['id']}";
            $user_info = $this->getORM()->queryAll($sql);
            return isset($user_info[0])?$user_info[0]:null;
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }


    /**
     * 获取角色信息
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function getRoleList()
    {
        try {
            $sql = "select * from u_role";
            $user_info = $this->getORM()->queryAll($sql);
            return $user_info;
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }


    /**
     * 插入数据
     * @param array $data
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertUserInfo($data)
    {
        try {
            if(isset($data['type']))
                $sql = "insert into u_role(`name`,`type`) VALUE ('{$data['name']}','{$data['type']}')";
            else
                $sql = "insert into u_role(`name`) VALUE ('{$data['name']}')";
//            echo $sql;exit;
            $user_info = $this->getORM()->queryAll($sql);
            $this -> addLog($this->getORM()->insert_id());
            return $user_info;
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }

    /**
     * 修改数据
     * @param array $data
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function updateUserInfo($data)
    {
        try {
            $sql = "update u_role set `name` = '{$data['name']}',`type` = '{$data['type']}' where id = {$data['id']}";
            $user_info = $this->getORM()->queryAll($sql);
            $this -> addLog($data['id']);
            return $user_info;
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }

    /**
     * 删除数据
     * @param array $data
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function deleteUserInfo($data)
    {
        try {
            $sql = "delete from u_role where id = {$data['id']} ";
            $user_info = $this->getORM()->queryAll($sql);
            $this -> addLog($data['id']);
            return $user_info;
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }

}